#include <head.h>

int main(int argc, char const *argv[])
{ // 打开并创建文件
    int fd = open("mian.txt", O_RDWR | O_CREAT, 0666);
    int fd_copy = open("copy.txt", O_RDWR | O_CREAT, 0666);
    char buf[1024];
    // 读取文件
    int byte = read(fd, buf, 249);
    buf[byte] = '\0';
    // printf("byte = %d\n", byte);
    // printf("%s\n", buf);
    // printf("===============================================\n");
    char buf1[1024];
    lseek(fd,249,SEEK_SET);
    int byte1 = read(fd, buf1, sizeof(buf));
    buf1[byte1] = '\0';
    // printf("%s\n", buf1);

    int pid = fork();

    if (pid > 0) // 0-249
    {   //flock(fd_copy,LOCK_EX);
        // printf("000\n");

        lseek(fd_copy, 0, SEEK_SET);
        byte = write(fd_copy, buf, byte);
        printf("第一部分写入完成\n");
        //flock(fd_copy,LOCK_UN);
        

    }

    if (pid == 0) // 448-249
    {   //flock(fd_copy,LOCK_EX);
        // printf("111\n");

        lseek(fd_copy, 0, SEEK_END);
        byte = write(fd_copy, buf1, byte1);

        printf("第二部分写入完成\n");
       // flock(fd_copy,LOCK_UN);
    }

    // while (1)
    // {
    //     sleep(1);
    // }

    close(fd);
    close(fd_copy);
    return 0;
}
